package com.example.loadingview.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import com.example.loadingview.ResourceTable;
import com.ldoublem.loadingviewlib.LVChromeLogo;
import com.ldoublem.loadingviewlib.LVCircular;
import com.ldoublem.loadingviewlib.LVCircularCD;
import com.ldoublem.loadingviewlib.LVLineWithText;
import com.ldoublem.loadingviewlib.LVSunSetView;
import com.ldoublem.loadingviewlib.view.LVBattery;
import com.ldoublem.loadingviewlib.view.LVBlazeWood;
import com.ldoublem.loadingviewlib.view.LVBlock;
import com.ldoublem.loadingviewlib.view.LVCircularJump;
import com.ldoublem.loadingviewlib.view.LVCircularRing;
import com.ldoublem.loadingviewlib.view.LVCircularSmile;
import com.ldoublem.loadingviewlib.view.LVCircularZoom;
import com.ldoublem.loadingviewlib.view.LVEatBeans;
import com.ldoublem.loadingviewlib.view.LVFinePoiStar;
import com.ldoublem.loadingviewlib.view.LVFunnyBar;
import com.ldoublem.loadingviewlib.view.LVGears;
import com.ldoublem.loadingviewlib.view.LVGearsTwo;
import com.ldoublem.loadingviewlib.view.LVGhost;
import com.ldoublem.loadingviewlib.view.LVNews;
import com.ldoublem.loadingviewlib.view.LVPlayBall;
import com.ldoublem.loadingviewlib.view.LVRingProgress;
import com.ldoublem.loadingviewlib.view.LVWifi;

import java.util.Timer;
import java.util.TimerTask;

public class MainAbilitySlice extends AbilitySlice {
    private int mValueLVLineWithText = 0;
    private int mValueLVNews = 0;

    private LVChromeLogo mLVChromeLogo;
    private LVCircular mLVCircular;
    private LVCircularCD mLVCircularCD;
    private LVLineWithText mLVLineWithText;
    private LVSunSetView mLVSunSetView;
    private LVBattery mLVBattery;
    private LVBlazeWood mLVBlazeWood;
    private LVBlock mLVBlock;
    private LVCircularJump mLVCircularJump;
    private LVCircularRing mLVCircularRing;
    private LVCircularSmile mLVCircularSmile;
    private LVCircularZoom mLVCircularZoom;
    private LVEatBeans mLVEatBeans;
    private LVFinePoiStar mLVFinePoiStar;
    private LVFunnyBar mLVFunnyBar;
    private LVGears mLVGears;
    private LVGearsTwo mLVGearsTwo;
    private LVGhost mLVGhost;
    private LVNews mLVNews;
    private LVPlayBall mLVPlayBall;
    private LVRingProgress mLVRingProgress;
    private LVWifi mLVWifi;

    public Timer mTimerLVLineWithText = new Timer();
    public Timer mTimerLVNews = new Timer();
    public HMEventHandler mEventHandler;
    public InnerEvent event;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        mEventHandler = new HMEventHandler(EventRunner.current());

        initComponent();
        initClickedListener();

    }

    private void initComponent() {
        mLVChromeLogo = (LVChromeLogo) findComponentById(ResourceTable.Id_LVChromeLogo);

        mLVCircular = (LVCircular) findComponentById(ResourceTable.Id_LVCircular);
        mLVCircular.setViewColor(Color.rgb(255, 99, 99));
        mLVCircular.setRoundColor(Color.rgb(255, 0, 0));

        mLVCircularCD = (LVCircularCD) findComponentById(ResourceTable.Id_LVCircularCD);
        mLVCircularCD.setViewColor(Color.rgb(0, 255, 0));

        mLVLineWithText = (LVLineWithText) findComponentById(ResourceTable.Id_LVLineWithText);
        mLVLineWithText.setViewColor(Color.rgb(33, 66, 77));
        mLVLineWithText.setTextColor(Color.rgb(233, 166, 177));

        mLVSunSetView = (LVSunSetView) findComponentById(ResourceTable.Id_LVSunSetView);
        mLVSunSetView.setSunstartTime("2021-03-10 07:00:00");
        mLVSunSetView.setSunendTime("2021-03-20 18:10:00");


        mLVBattery = (LVBattery) findComponentById(ResourceTable.Id_LVBattery);
        mLVBattery.setBatteryOrientation(LVBattery.BatteryOrientation.VERTICAL);//LVBattery.BatteryOrientation.HORIZONTAL
        mLVBattery.setShowNum(true);

        mLVBattery.setViewColor(Color.WHITE.getValue());
        mLVBattery.setCellColor(Color.GREEN.getValue());

        mLVBlazeWood = (LVBlazeWood) findComponentById(ResourceTable.Id_LVBlazeWood);

        mLVBlock = (LVBlock) findComponentById(ResourceTable.Id_LVBlock);

        mLVCircularJump = (LVCircularJump) findComponentById(ResourceTable.Id_LVCircularJump);
        mLVCircularJump.setViewColor(Color.rgb(0, 0, 0));

        mLVCircularRing = (LVCircularRing) findComponentById(ResourceTable.Id_LVCircularRing);
        mLVCircularRing.setViewColor(Color.argb(100, 255, 255, 255));
        mLVCircularRing.setBarColor(Color.YELLOW.getValue());

        mLVCircularSmile = (LVCircularSmile) findComponentById(ResourceTable.Id_LVCircularSmile);
        mLVCircularSmile.setViewColor(Color.rgb(144, 238, 146));

        mLVCircularZoom = (LVCircularZoom) findComponentById(ResourceTable.Id_LVCircularZoom);
        mLVCircularZoom.setViewColor(Color.rgb(255, 0, 122));

        mLVEatBeans = (LVEatBeans) findComponentById(ResourceTable.Id_LVEatBeans);
        mLVEatBeans.setViewColor(Color.WHITE.getValue());
        mLVEatBeans.setEyeColor(Color.BLUE.getValue());

        mLVFinePoiStar = (LVFinePoiStar) findComponentById(ResourceTable.Id_LVFinePoiStar);
        mLVFinePoiStar.setViewColor(Color.WHITE.getValue());
        mLVFinePoiStar.setCircleColor(Color.YELLOW.getValue());
        mLVFinePoiStar.setDrawPath(true);

        mLVFunnyBar = (LVFunnyBar) findComponentById(ResourceTable.Id_LVFunnyBar);
        mLVFunnyBar.setViewColor(Color.rgb(234, 167, 107));

        mLVGears = (LVGears) findComponentById(ResourceTable.Id_LVGears);
        mLVGears.setViewColor(Color.rgb(55, 155, 233));

        mLVGearsTwo = (LVGearsTwo) findComponentById(ResourceTable.Id_LVGearsTwo);
        mLVGearsTwo.setViewColor(Color.rgb(155, 55, 233));

        mLVGhost = (LVGhost) findComponentById(ResourceTable.Id_LVGhost);
        mLVGhost.setViewColor(Color.WHITE.getValue());
        mLVGhost.setHandColor(Color.BLACK.getValue());

        mLVNews = (LVNews) findComponentById(ResourceTable.Id_LVNews);
        mLVNews.setViewColor(Color.WHITE.getValue());

        mLVPlayBall = (LVPlayBall) findComponentById(ResourceTable.Id_LVPlayBall);
        mLVPlayBall.setViewColor(Color.WHITE.getValue());
        mLVPlayBall.setBallColor(Color.RED.getValue());

        mLVRingProgress = (LVRingProgress) findComponentById(ResourceTable.Id_LVRingProgress);
        mLVRingProgress.setViewColor(Color.WHITE.getValue());
        mLVRingProgress.setTextColor(Color.BLACK.getValue());
        mLVRingProgress.setPorBarStartColor(Color.YELLOW.getValue());
        mLVRingProgress.setPorBarEndColor(Color.BLUE.getValue());

        mLVWifi = (LVWifi) findComponentById(ResourceTable.Id_LVWifi);
        mLVWifi.setViewColor(Color.BLACK.getValue());
    }


    private void initClickedListener() {
        mLVChromeLogo.setClickedListener(component -> mLVChromeLogo.startAnim());
        mLVCircular.setClickedListener(component -> mLVCircular.startAnim());
        mLVCircularCD.setClickedListener(component -> mLVCircularCD.startAnim());
        mLVLineWithText.setClickedListener(component -> startLVLineWithTextAnim());
        mLVSunSetView.setClickedListener(component -> mLVSunSetView.startSunset());
        mLVBattery.setClickedListener(component -> mLVBattery.startAnim(5000));
        mLVBlazeWood.setClickedListener(component -> mLVBlazeWood.startAnim());
        mLVBlock.setClickedListener(component -> mLVBlock.startAnim());
        mLVCircularJump.setClickedListener(component -> mLVCircularJump.startAnim());
        mLVCircularRing.setClickedListener(component -> mLVCircularRing.startAnim());
        mLVCircularSmile.setClickedListener(component -> mLVCircularSmile.startAnim(1000));
        mLVCircularZoom.setClickedListener(component -> mLVCircularZoom.startAnim());
        mLVEatBeans.setClickedListener(component -> mLVEatBeans.startAnim(3500));
        mLVFunnyBar.setClickedListener(component -> mLVFunnyBar.startAnim());
        mLVGears.setClickedListener(component -> mLVGears.startAnim());
        mLVGearsTwo.setClickedListener(component -> mLVGearsTwo.startAnim());
        mLVGhost.setClickedListener(component -> mLVGhost.startAnim());
        mLVPlayBall.setClickedListener(component -> mLVPlayBall.startAnim());
        mLVRingProgress.setClickedListener(component -> mLVRingProgress.startAnim(3000));
        mLVWifi.setClickedListener(component -> mLVWifi.startAnim(9000));
        mLVNews.setClickedListener(component -> startLVNewsAnim());
        mLVFinePoiStar.setClickedListener(component -> {
            mLVFinePoiStar.setDrawPath(false);
            mLVFinePoiStar.startAnim(5000);
        });
    }

    private void startLVLineWithTextAnim() {
        mValueLVLineWithText = 0;
        if (mTimerLVLineWithText != null) {
            mTimerLVLineWithText.cancel();
        }
        mTimerLVLineWithText = new Timer();
        timerTaskLVLineWithText();
    }

    public void timerTaskLVLineWithText() {
        mTimerLVLineWithText.schedule(new TimerTask() {
            @Override
            public void run() {
                if (mValueLVLineWithText < 100) {

                    mValueLVLineWithText++;

                    int eventId = 2;
                    long param = mValueLVLineWithText;
                    Object object = null;
                    event = InnerEvent.get(eventId, param, object);
                    mEventHandler.sendEvent(event, 0, EventHandler.Priority.IMMEDIATE);

                } else {
                    mTimerLVLineWithText.cancel();
                }
            }
        }, 0, 50);
    }

    public class HMEventHandler extends EventHandler {
        public HMEventHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            int id = event.eventId;
            long param = event.param;
            if(id == 2){
                mLVLineWithText.setValue((int) param);
            }else if(id == 1){
                mLVNews.setValue((int) param);
            }

        }
    }

    private void startLVNewsAnim() {
        mValueLVNews = 0;
        if (mTimerLVNews != null) {

            mTimerLVNews.cancel();
        }
        mTimerLVNews = new Timer();
        timerTaskLVNews();
    }

    public void timerTaskLVNews() {
        mTimerLVNews.schedule(new TimerTask() {
            @Override
            public void run() {
                if (mValueLVNews < 100) {
                    mValueLVNews++;
                    int eventId = 1;
                    long param = mValueLVNews;
                    Object object = null;
                    event = InnerEvent.get(eventId, param, object);
                    mEventHandler.sendEvent(event, 0, EventHandler.Priority.IMMEDIATE);
                } else {
                    mTimerLVNews.cancel();
                }
            }
        }, 0, 10);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
